var express = require('express');
var path = require('path');
var router = express.Router();

var query = require('./pool');
var bodyParser = require('body-parser');
var urlEncoded = bodyParser.urlencoded({ extended: false });

router.get('/', function (req, res) {
    let pathStr = path.normalize(__dirname + '\/../views/pay.html');
    res.sendFile(pathStr);
});


router.post('/p', urlEncoded, async (req, res) => {
    console.log('wwwwwwwwwwwwwwwwwwwwww');
    console.log(req.body, '==================');
    if (req.body) {
        let sql = 'insert into sp_car set ?';
        let car = await query(sql, req.body, (error, results, fields) => {
            if (error) throw error;
        });
        let json = {
            code: '100',
            cara: car
        };
        res.send(json);
    } else {
        let json = {
            code: 0
        };
        res.send(json);
    }
});

module.exports = router;